﻿using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Net;
using System.Text;
using System.Web;
using System.Collections.Generic;

namespace lxwmvc
{
    /// <summary>
    /// p2p公共方法
    /////按季付息，到期一次性还本付息 512
    /////先息后本 513
    /////按天计息，到期一次性还本付息 488
    /////到期一次性还本付息 415
    /// </summary>
    class core_p2p
    {

        /// <summary>
        /// 计算器，基本上不用了  lxw 里面的还款类型全变了
        /// </summary>
        /// <param name="investment">投资金额</param>
        /// <param name="earnings">年收益率</param>
        /// <param name="dtmStart">起始日期</param>
        /// <param name="dtmEnd">结束日期</param>
        /// <param name="investmenttype">还款方式</param>
        /// <param name="zlx">总利息</param>
        /// <returns></returns>
        public static DataTable Tools(double investment, double earnings, DateTime dtmStart, DateTime dtmEnd, int investmenttype, out double zlx)
        {

            int mon = 0;//月数
            int day = 0;//不足月 天数
            zlx = 0.00;

            #region 计算 月、天
            DateTime dtm = dtmStart;
            for (int i = 1; i < 2000; i++)
            {
                if (dtm.AddMonths(i).AddDays(-1) <= dtmEnd)
                {
                    continue;
                }
                if (dtm.AddMonths(i).AddDays(-1) > dtmEnd)
                {
                    mon = i - 1;
                    day = (dtmEnd - dtm.AddMonths(i - 1)).Days + 1;
                    break;
                }
            }
            #endregion

            int timelimitmon = mon;
            if (day != 0)
            {
                timelimitmon++;
            }

            //上面计算相差几个月，公式太复杂了。

            //计算30天的利息
            double earningsmon = (earnings / 365 / 100) * 30;

            double lx = 0.0;//每个月利息
            DataTable dt = new DataTable();
            dt.Columns.Add("qishu", typeof(string));
            dt.Columns.Add("benxi", typeof(double));
            dt.Columns.Add("benjin", typeof(double));
            dt.Columns.Add("lixi", typeof(double));
            dt.Columns.Add("daoqiri", typeof(DateTime));
            dt.Columns.Add("yue", typeof(double));
            DataRow updaterow;

            double mybj = investment / timelimitmon;//每个月应还的本金 
            
            //还款方式为 按月还款等额本息
            if (investmenttype == 410)
            {
                double benxi = (investment * earningsmon * Math.Pow(1 + earningsmon, timelimitmon)) / (Math.Pow(1 + earningsmon, timelimitmon) - 1);
                for (int i = 1; i <= timelimitmon; i++)
                {
                    lx = Math.Round(investment * earningsmon, 2); //每个月应还的利息 
                    investment = Math.Round(investment - Math.Round(benxi - lx, 2), 2);
                    zlx += lx;//12个月总利息

                    updaterow = dt.NewRow();
                    updaterow["qishu"] = i.tostr();
                    updaterow["benxi"] = Math.Round(benxi, 2).tostr();
                    updaterow["benjin"] = Math.Round(benxi - lx, 2).tostr();
                    updaterow["lixi"] = lx.tostr();
                    updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                    if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                    {
                        updaterow["daoqiri"] = dtmEnd;
                    }

                    updaterow["yue"] = investment.tostr();
                    if (i == timelimitmon)
                    {
                        updaterow["yue"] = "0.00";
                    }
                    dt.Rows.Add(updaterow);
                }
            }

            //还款方式为 按月还款等额本金
            if (investmenttype == 513)
            {
                for (int i = 1; i <= timelimitmon; i++)
                {
                    lx = Math.Round(investment * earningsmon, 2); //每个月应还的利息 
                    investment = Math.Round(investment - mybj, 2);
                    zlx += lx;//12个月总利息

                    updaterow = dt.NewRow();
                    updaterow["qishu"] = i.tostr();
                    updaterow["benxi"] = (lx + Math.Round(mybj, 2)).tostr();
                    updaterow["benjin"] = Math.Round(mybj, 2).tostr();
                    updaterow["lixi"] = lx.tostr();
                    updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                    if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                    {
                        updaterow["daoqiri"] = dtmEnd;
                    }
                    updaterow["yue"] = investment.tostr();
                    if (i == timelimitmon)
                    {
                        updaterow["yue"] = "0.00";
                    }

                    dt.Rows.Add(updaterow);
                }
            }

            //还款方式为 一次性还款
            if (investmenttype == 412)
            {
                zlx = Math.Round(Math.Round(investment * earningsmon, 2) * timelimitmon, 2);
                lx = Math.Round(investment * earningsmon, 2); //每个月应还的利息  
                for (int i = 1; i <= timelimitmon; i++)
                {
                    updaterow = dt.NewRow();
                    updaterow["qishu"] = i.tostr();
                    updaterow["benxi"] = lx.tostr();
                    updaterow["benjin"] = "0.00";
                    updaterow["lixi"] = lx.tostr();
                    updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                    if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                    {
                        updaterow["daoqiri"] = dtmEnd;
                    }
                    updaterow["yue"] = investment.tostr();
                    if (i == timelimitmon)
                    {
                        updaterow["benxi"] = (investment + lx).tostr();
                        updaterow["benjin"] = investment.tostr();
                        updaterow["lixi"] = lx.tostr();
                        updaterow["yue"] = "0.00";
                    }
                    dt.Rows.Add(updaterow);
                }
            }

            //一次性付息到期还本
            if (investmenttype == 413)
            {
                zlx = Math.Round(Math.Round(investment * earningsmon, 2) * timelimitmon, 2);
                for (int i = 1; i <= timelimitmon; i++)
                {
                    updaterow = dt.NewRow();
                    updaterow["qishu"] = i.tostr();
                    if (i == 1)
                    {
                        updaterow["benxi"] = zlx.tostr();
                        updaterow["benjin"] = "0.00";
                        updaterow["lixi"] = zlx.tostr();
                        updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                        if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                        {
                            updaterow["daoqiri"] = dtmEnd;
                        }
                        updaterow["yue"] = investment.tostr();
                    }
                    else
                    {
                        updaterow["benxi"] = "0.00";
                        updaterow["benjin"] = "0.00";
                        updaterow["lixi"] = "0.00";
                        updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                        if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                        {
                            updaterow["daoqiri"] = dtmEnd;
                        }
                        updaterow["yue"] = investment.tostr();
                    }
                    if (i == timelimitmon)
                    {
                        updaterow["benxi"] = investment.tostr();
                        updaterow["benjin"] = investment.tostr();
                        updaterow["lixi"] = "0.00";
                        updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                        if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                        {
                            updaterow["daoqiri"] = dtmEnd;
                        }
                        updaterow["yue"] = "0.00";
                    }
                    dt.Rows.Add(updaterow);
                }
            }

            //到期统一还本息
            if (investmenttype == 415)
            {
                zlx = Math.Round(Math.Round(investment * earningsmon, 2) * timelimitmon, 2);
                for (int i = 1; i <= timelimitmon; i++)
                {
                    updaterow = dt.NewRow();
                    updaterow["qishu"] = i.tostr();
                    updaterow["benxi"] = "0.00";
                    updaterow["benjin"] = "0.00";
                    updaterow["lixi"] = "0.00";
                    updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                    if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                    {
                        updaterow["daoqiri"] = dtmEnd;
                    }
                    updaterow["yue"] = investment.tostr();
                    if (i == timelimitmon)
                    {
                        updaterow["benxi"] = (investment + zlx).tostr();
                        updaterow["benjin"] = investment.tostr();
                        updaterow["lixi"] = zlx.tostr();
                        updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                        if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                        {
                            updaterow["daoqiri"] = dtmEnd;
                        }
                        updaterow["yue"] = "0.00";
                    }
                    dt.Rows.Add(updaterow);
                }
            }

            return dt;
        }

        
        #region P2P根据投资额度生成收益计划
        /// <summary>
        /// P2P根据投资额度生成收益计划
        /// </summary>
        /// <param name="amount">投资金额</param>
        /// <param name="rate">年化利率</param>
        /// <param name="mounth">投资期限（月）</param>
        /// <param name="repayment">还款方式</param>
        /// <param name="dateime">计息时间</param>
        /// <returns></returns>
        public static IList<PBenefitPlan> GetBenefitPlansByInvestmentAmount(int pjktzjlid, int pjkjbxxid, string userId, decimal amount, decimal youhuijine, decimal rate, int mounth, int repayment, DateTime dateime)
        {
            //通过还款表，生成每个人的还款计划，这个地方按照期数排列
            var hkList = queryhelper.query<_phk>(" SELECT * FROM phk WHERE pjkjbxxid= " + pjkjbxxid + " order by qishu asc").AsList<_phk>();
            //按季付息，到期一次性还本付息 512
            //先息后本 513
            //按天计息，到期一次性还本付息 488
            //到期一次性还本付息 415
             

            IList<PBenefitPlan> plans = new List<PBenefitPlan>();

            if (repayment == 513)//按天计息
            {
                PBenefitPlan plan = null;
                for (int i = 1; i <= mounth; i++)
                {
                    plan = new PBenefitPlan();
                    plan.phkid = hkList[i - 1].phkid;
                    plan.Deadline = dateime.AddMonths(i);
                    plan.PlanStatus = 0;
                    plan.Principal = 0;
                    // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
                    plan.Interest = Math.Round(amount * 30 * rate / 365, 2);
                    plan.ProjectID = pjktzjlid;
                    plan.UserID = userId;

                    if (i == mounth)//最后一个月还本
                    {
                        plan.Principal = amount - youhuijine;//本金                       
                    }
                    plans.Add(plan);
                }
            }
            else if (repayment == 415)
            {
                PBenefitPlan plan = new PBenefitPlan();
                plan.phkid = hkList[0].phkid;
                plan.UserID = userId;
                plan.ProjectID = pjktzjlid;
                plan.Principal = amount - youhuijine;
                plan.PlanStatus = 0;
                plan.Deadline = dateime.AddMonths(mounth);
                plan.Interest = Math.Round(amount * 30 * mounth * rate / 365, 2);
                plans.Add(plan);
            }
            else if (repayment == 481 || repayment == 488)
            {
                PBenefitPlan plan = new PBenefitPlan();
                plan.phkid = hkList[0].phkid;
                plan.UserID = userId;
                plan.ProjectID = pjktzjlid;
                plan.Principal = amount - youhuijine;
                plan.PlanStatus = 0;
                plan.Deadline = dateime.AddDays(mounth);
                plan.Interest = Math.Round(amount * mounth * rate / 365, 2);
                plans.Add(plan);
            }
            //等额本金计算方式
            else if (repayment == 888)
            {
                //PBenefitPlan plan = new PBenefitPlan();
                //plan.phkid = hkList.Rows[0][0].toint32();
                //plan.UserID = userId;
                //plan.ProjectID = projectId;
                //plan.Principal = amount - youhuijine;
                //plan.PlanStatus = 0;
                //plan.Deadline = dateime.AddDays(mounth);
                //plan.Interest = Math.Round(amount * mounth * rate / 365, 2);
                //plans.Add(plan);

                List<PBenefitPlan> listplan = new List<PBenefitPlan>();
                double[][] arr = new double[3][];//声明交错数组

                int i;
                double tlnAcct = 0, tdepAcct = 0;

                double[] lnAcctbal = new double[mounth]; /*贷款余额*/
                double[] depAcctbal = new double[mounth]; /*总还款*/
                double[] payrateAcct = new double[mounth]; /*每月应还利息*/
                double[] payAcct = new double[mounth]; /*每月应还款*/
                double[] paybaseAcct = new double[mounth]; /*每月应还本金*/

                tlnAcct = amount.todouble();

                for (i = 0; i < mounth; i++)
                {
                    PBenefitPlan plan = new PBenefitPlan();

                    paybaseAcct[i] = Math.Round((Math.Pow((1 + (double)rate / 365 * 30), i + 1) - Math.Pow((1 + (double)rate / 365 * 30), i)) / (Math.Pow((1 + (double)rate / 365 * 30), mounth) - 1) * amount.todouble(), 2);
                    plan.Principal = paybaseAcct[i].todecimal();//每月本金
                    payrateAcct[i] = Math.Round(tlnAcct * (double)rate / 365 * 30, 2);
                    plan.Interest = payrateAcct[i].todecimal();//每月本息
                    payAcct[i] = Math.Round(paybaseAcct[i] + payrateAcct[i], 2);

                    lnAcctbal[i] = tlnAcct - paybaseAcct[i];
                    depAcctbal[i] = tdepAcct + payAcct[i];
                    tdepAcct = depAcctbal[i];
                    tlnAcct = tlnAcct - paybaseAcct[i];

                    plan.phkid = hkList[0].phkid;
                    plan.UserID = userId;
                    plan.ProjectID = pjktzjlid;
                    plan.PlanStatus = 0;
                    plan.Deadline = dateime.AddDays(i + 1);

                    plans.Add(plan);
                }

                //arr[0] = payAcct;//月还款
                //arr[1] = paybaseAcct;//月本金
                //arr[2] = payrateAcct;//月利息

            }
            //等额本息计算方式
            else if (repayment == 888)
            {
                //PBenefitPlan plan = new PBenefitPlan();
                //plan.phkid = hkList.Rows[0][0].toint32();
                //plan.UserID = userId;
                //plan.ProjectID = projectId;
                //plan.Principal = amount - youhuijine;
                //plan.PlanStatus = 0;
                //plan.Deadline = dateime.AddDays(mounth);
                //plan.Interest = Math.Round(amount * mounth * rate / 365, 2);
                //plans.Add(plan);

                List<PBenefitPlan> listplan = new List<PBenefitPlan>();
                double[][] arr = new double[2][];//声明交错数组

                double[] fPrincipal = new double[mounth]; //本金
                double[] fSparePrin = new double[mounth];//剩余本金
                double[] fMonthRate = new double[mounth];//月利息
                double[] fMonthMoney = new double[mounth];//月还款额


                for (int i = 0; i < mounth; i++)
                {
                    PBenefitPlan plan = new PBenefitPlan();
                    fPrincipal[i] = (double)(amount / mounth);
                    if (i == 0)
                    {
                        fSparePrin[i] = (double)amount - fPrincipal[i];//剩余本金
                        fMonthRate[i] = Math.Round((double)amount * ((double)rate / 365 * 30), 2);//月利息
                        plan.Interest = fMonthRate[i].todecimal();//月利息
                        plan.phkid = hkList[0].phkid;
                        plan.UserID = userId;
                        plan.ProjectID = pjktzjlid;
                        plan.Principal = Math.Round(fPrincipal[i], 2).todecimal();
                        plan.PlanStatus = 0;
                        plan.Deadline = dateime.AddDays(i + 1);
                    }
                    else
                    {
                        fSparePrin[i] = fSparePrin[i - 1] - fPrincipal[i];//剩余本金
                        fMonthRate[i] = Math.Round(fSparePrin[i - 1] * ((double)rate / 365 * 30), 2);//月利息=上个月的剩余本金×利率
                        plan.Interest = fMonthRate[i].todecimal();
                        plan.phkid = hkList[0].phkid;
                        plan.UserID = userId;
                        plan.ProjectID = pjktzjlid;
                        plan.Principal = Math.Round(fPrincipal[i], 2).todecimal();
                        plan.PlanStatus = 0;
                        plan.Deadline = dateime.AddDays(i + 1);
                    }

                    fMonthMoney[i] = Math.Round(fPrincipal[i] + fMonthRate[i], 2);

                    //model.Principal = Math.Round(fPrincipal[i], 2);
                    //model.LastDate = startingDate.AddMonths(i + 1);

                    plans.Add(plan);
                }

                //arr[0] = fMonthMoney;//月还款
                //arr[1] = fMonthRate;//月利息
            }
            else
            {
                PBenefitPlan plan = null;
                for (int i = 1; i <= mounth; i++)
                {
                    plan = new PBenefitPlan();
                    if (i != mounth)
                    {
                        plan.phkid = hkList[i-1].phkid;
                        plan.Deadline = dateime.AddMonths(i);
                        plan.PlanStatus = 0;
                        plan.Principal = 0;
                        // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
                        plan.Interest = Math.Round(amount * 30 * rate / 365, 2);
                        plan.ProjectID = pjktzjlid;
                        plan.UserID = userId;
                    }
                    else
                    {
                        plan.phkid = hkList[i - 1].phkid;
                        plan.Deadline = dateime.AddMonths(i);
                        plan.PlanStatus = 0;
                        plan.Principal = amount - youhuijine;//本金
                        // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
                        plan.Interest = Math.Round(amount * 30 * rate / 365, 2);
                        plan.ProjectID = pjktzjlid;
                        plan.UserID = userId;
                    }
                    plans.Add(plan);
                }
            }
            return plans;
        }

        /// <summary>
        /// 得到还款方式
        /// </summary>
        /// <param name="key"></param>
        /// <returns></returns>
        public static string GetHuanKuanFangShiName(string key)
        {
            string name = "";
            switch (key)
            {
                case "410":
                    name = "按月等额本息还款"; break;
                case "411":
                    name = "按月等额本金还款"; break;
                case "513":
                    name = "先息后本"; break;
                case "413":
                    name = "一次性付息到期还本"; break;
                case "415":
                    name = "到期一次性还本还息"; break;
                case "481":
                    name = "按天计息，到期一次性还本付息"; break;
                case "488":
                    name = "按天计息，到期一次性还本付息"; break;
            }
            return name;
        }

        /// <summary>
        /// 按照天，计算本机和利息
        /// </summary>
        /// <param name="investment"></param>
        /// <param name="earnings"></param>
        /// <param name="days"></param>
        /// <param name="dtmEnd"></param>
        /// <param name="investmenttype"></param>
        /// <param name="zlx"></param>
        /// <returns></returns>
        public static DataTable JiSuanShouYiByDay(double investment, double earnings, int days, DateTime dtmEnd, int investmenttype, out double zlx)
        {
            earnings = earnings / 100;
            DataTable dt = new DataTable();
            dt.Columns.Add("qishu", typeof(string));
            dt.Columns.Add("benxi", typeof(double));
            dt.Columns.Add("benjin", typeof(double));
            dt.Columns.Add("lixi", typeof(double));
            dt.Columns.Add("daoqiri", typeof(DateTime));
            dt.Columns.Add("yue", typeof(double));
            zlx = Math.Round(investment * days * earnings / 365, 2);
            if (days != 0)
            {
                DataRow updaterow = dt.NewRow();
                updaterow["qishu"] = 1;
                updaterow["benjin"] = investment;

                updaterow["lixi"] = zlx;
                updaterow["benxi"] = updaterow["benjin"].todouble() + updaterow["lixi"].todouble();
                updaterow["daoqiri"] = dtmEnd;
                updaterow["yue"] = "0";
                dt.Rows.Add(updaterow);
            }
            return dt;
        }

        public static DataTable JiSuanShouYiByMonth(double investment, int month, double earnings, DateTime dtmStart, DateTime dtmEnd, int investmenttype, out double zlx)
        {
            //int mon = 0;//月数
            //int day = 0;//不足月 天数
            zlx = 0.00;

            #region 计算 月、天
            //DateTime dtm = dtmStart;
            //for (int i = 1; i < 2000; i++)
            //{
            //    if (dtm.AddMonths(i).AddDays(-1) <= dtmEnd)
            //    {
            //        continue;
            //    }
            //    if (dtm.AddMonths(i).AddDays(-1) > dtmEnd)
            //    {
            //        mon = i - 1;
            //        day = (dtmEnd - dtm.AddMonths(i - 1)).Days + 1;
            //        break;
            //    }
            //}
            #endregion

            //int timelimitmon = mon;
            //if (day != 0)
            //{
            //timelimitmon++;
            //}

            double earningsmon = (earnings / 365 / 100) * 30;

            double lx = 0.0;//每个月利息
            DataTable dt = new DataTable();
            dt.Columns.Add("qishu", typeof(string));
            dt.Columns.Add("benxi", typeof(double));
            dt.Columns.Add("benjin", typeof(double));
            dt.Columns.Add("lixi", typeof(double));
            dt.Columns.Add("daoqiri", typeof(DateTime));
            dt.Columns.Add("yue", typeof(double));
            DataRow updaterow;

            double mybj = investment / month;//每个月应还的本金 

            #region 舍去的还款方式

            //还款方式为 按月还款等额本息
            //if (investmenttype == 410)
            //{
            //    double benxi = (investment * earningsmon * Math.Pow(1 + earningsmon, timelimitmon)) / (Math.Pow(1 + earningsmon, timelimitmon) - 1);
            //    for (int i = 1; i <= timelimitmon; i++)
            //    {
            //        lx = Math.Round(investment * earningsmon, 2); //每个月应还的利息 
            //        investment = Math.Round(investment - Math.Round(benxi - lx, 2), 2);
            //        zlx += lx;//12个月总利息

            //        updaterow = dt.NewRow();
            //        updaterow["qishu"] = i.tostr();
            //        updaterow["benxi"] = Math.Round(benxi, 2).tostr();
            //        updaterow["benjin"] = Math.Round(benxi - lx, 2).tostr();
            //        updaterow["lixi"] = lx.tostr();
            //        updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
            //        if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
            //        {
            //            updaterow["daoqiri"] = dtmEnd;
            //        }

            //        updaterow["yue"] = investment.tostr();
            //        if (i == timelimitmon)
            //        {
            //            updaterow["yue"] = "0.00";
            //        }
            //        dt.Rows.Add(updaterow);
            //    }
            //}
            //还款方式为 按月还款等额本金
            //if (investmenttype == 411)
            //{
            //    for (int i = 1; i <= timelimitmon; i++)
            //    {
            //        lx = Math.Round(investment * earningsmon, 2); //每个月应还的利息 
            //        investment = Math.Round(investment - mybj, 2);
            //        zlx += lx;//12个月总利息

            //        updaterow = dt.NewRow();
            //        updaterow["qishu"] = i.tostr();
            //        updaterow["benxi"] = (lx + Math.Round(mybj, 2)).tostr();
            //        updaterow["benjin"] = Math.Round(mybj, 2).tostr();
            //        updaterow["lixi"] = lx.tostr();
            //        updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
            //        if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
            //        {
            //            updaterow["daoqiri"] = dtmEnd;
            //        }
            //        updaterow["yue"] = investment.tostr();
            //        if (i == timelimitmon)
            //        {
            //            updaterow["yue"] = "0.00";
            //        }
            //        dt.Rows.Add(updaterow);
            //    }
            //}
            //一次性付息到期还本
            //if (investmenttype == 413)
            //{
            //    zlx = Math.Round(Math.Round(investment * earningsmon, 2) * timelimitmon, 2);
            //    for (int i = 1; i <= timelimitmon; i++)
            //    {
            //        updaterow = dt.NewRow();
            //        updaterow["qishu"] = i.tostr();
            //        if (i == 1)
            //        {
            //            updaterow["benxi"] = zlx.tostr();
            //            updaterow["benjin"] = "0.00";
            //            updaterow["lixi"] = zlx.tostr();
            //            updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
            //            if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
            //            {
            //                updaterow["daoqiri"] = dtmEnd;
            //            }
            //            updaterow["yue"] = investment.tostr();
            //        }
            //        else
            //        {
            //            updaterow["benxi"] = "0.00";
            //            updaterow["benjin"] = "0.00";
            //            updaterow["lixi"] = "0.00";
            //            updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
            //            if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
            //            {
            //                updaterow["daoqiri"] = dtmEnd;
            //            }
            //            updaterow["yue"] = investment.tostr();
            //        }
            //        if (i == timelimitmon)
            //        {
            //            updaterow["benxi"] = investment.tostr();
            //            updaterow["benjin"] = investment.tostr();
            //            updaterow["lixi"] = "0.00";
            //            updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
            //            if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
            //            {
            //                updaterow["daoqiri"] = dtmEnd;
            //            }
            //            updaterow["yue"] = "0.00";
            //        }
            //        dt.Rows.Add(updaterow);
            //    }
            //}
            #endregion

            //还款方式为 一次性还款
            if (investmenttype == 513)
            {
                zlx = Math.Round(Math.Round(investment * earningsmon, 2) * month, 2);
                lx = Math.Round(investment * earningsmon, 2); //每个月应还的利息  
                for (int i = 1; i <= month; i++)
                {
                    updaterow = dt.NewRow();
                    updaterow["qishu"] = i.tostr();
                    updaterow["benxi"] = lx.tostr();
                    updaterow["benjin"] = "0.00";
                    updaterow["lixi"] = lx.tostr();
                    updaterow["daoqiri"] = dtmStart.AddMonths(i).AddDays(-1);
                    if (dtmStart.AddMonths(i).AddDays(-1) > dtmEnd)
                    {
                        updaterow["daoqiri"] = dtmEnd;
                    }
                    updaterow["yue"] = investment.tostr();
                    if (i == month)
                    {
                        updaterow["benxi"] = (investment + lx).tostr();
                        updaterow["benjin"] = investment.tostr();
                        updaterow["lixi"] = lx.tostr();
                        updaterow["yue"] = "0.00";
                    }
                    dt.Rows.Add(updaterow);
                }
            }

            //到期统一还本息
            if (investmenttype == 415)
            {
                zlx = Math.Round(Math.Round(investment * earningsmon, 2) * month, 2);
                //for (int i = 1; i <= timelimitmon; i++)
                //{
                updaterow = dt.NewRow();
                updaterow["qishu"] = 1;
                if (dtmStart.AddMonths(month).AddDays(-1) > dtmEnd)
                {
                    updaterow["daoqiri"] = dtmEnd;
                }
                updaterow["yue"] = investment.tostr();
                updaterow["benxi"] = (investment + zlx).tostr();
                updaterow["benjin"] = investment.tostr();
                updaterow["lixi"] = zlx.tostr();
                updaterow["daoqiri"] = dtmStart.AddMonths(month).AddDays(-1);
                if (dtmStart.AddMonths(month).AddDays(-1) > dtmEnd)
                {
                    updaterow["daoqiri"] = dtmEnd;
                }
                updaterow["yue"] = "0.00";

                dt.Rows.Add(updaterow);

            }

            return dt;
        }
        /// <summary>
        /// 根据债券投资金额获取相应的投资
        /// </summary>
        /// <param name="pjkjbxxid">投资项目编号</param>
        /// <param name="newpjktzjlid">债权购买人投资记录ID</param>
        /// <param name="pjktzjlid">债权出售人原先投资记录ID</param>
        /// <param name="yhids"></param>
        /// <param name="touzirenid">投资人ID</param>
        /// <param name="touzirenid">出售人ID</param>
        /// <param name="tzjine">债权购买额度</param>
        /// <param name="tzjineyh">优惠金额</param>
        /// <param name="rate">利率</param>
        /// <returns></returns>
        //public static IList<PBenefitPlan> GetBenefitPlansByDebtInvestmentAmountForOffLine(int pjkjbxxid, int newpjktzjlid, int pjktzjlid, string touzirenid, string shouchurenid, decimal tzjine, decimal rate)
        //{
        //    IList<PBenefitPlan> planList = new List<PBenefitPlan>();

        //    rate = rate / 100;
        //    //把债权转让人的投资记录金额变为转变后的金额
        //    jiandansql.execute(" UPDATE pjktzjl SET toubiaojine=toubiaojine-" + tzjine + ",zhaiquanjine=zhaiquanjine+" + tzjine + " WHERE pjktzjlid=" + pjktzjlid);
        //    //把债权转让人的收益计划中未收益的收益计划状态变为“转让为债权”
        //    jiandansql.execute(" UPDATE PBenefitPlan SET PlanStatus=3 WHERE PlanStatus=0 AND pjktzjlid=" + pjktzjlid);

        //    //原标编号pjkjbxxid，原标投资记录pjktzjlid
        //    int zhaiquanrenid = jiandansql.getint("SELECT toubiaorenid FROM pjktzjl WHERE pjktzjlid=" + pjktzjlid);
        //    decimal oldtouziedu = jiandansql.getdecimal("SELECT toubiaojine FROM pjktzjl WHERE pjktzjlid=" + pjktzjlid);
        //    var projrct = jiandansql.getonerow("SELECT * FROM pjkjbxx WHERE pjkjbxxid=" + pjkjbxxid);
        //    var bengxiliangqing = Convert.ToDateTime(projrct["huankuanqixian"]).AddDays(-1);//还款期限
        //    var hkList = jiandansql.gettable(" SELECT * FROM [dbo].[phk] WHERE pjkjbxxid= " + pjkjbxxid);//原标还款计划


        //    int tianshu = 0;
        //    DateTime jiexiriqi;

        //    #region 债权转让人收益计划

        //    //for (int i = 0; i < hkList.Rows.Count; i++)
        //    //{
        //    //    DataRow item = hkList.Rows[i];
        //    //    PBenefitPlan plan;
        //    //    jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //    //    //计算天数
        //    //    int intDays = 0;
        //    //    intDays = (int)(jiexiriqi - DateTime.Now.Date).TotalDays;
        //    //    if (intDays <= 0)
        //    //    {
        //    //        continue;
        //    //    }
        //    //    //int intday = (int)(jiexiriqi - Convert.ToDateTime(bengxiliangqing)).TotalDays;
        //    //    //if (intday<= 0)
        //    //    //{
        //    //    //    continue;
        //    //    //}
        //    //    tianshu += 1;
        //    //    //如果债券卖出日期是在结息日的前面
        //    //    if (tianshu == 1)
        //    //    {
        //    //        //查找上个月还款日期
        //    //        DateTime shangyuejiexi = Convert.ToDateTime(item["huankuanriqi"]).AddMonths(-1);

        //    //        int chashangdays = (int)(DateTime.Now - shangyuejiexi).TotalDays;
        //    //        int chaxiadays = (int)(Convert.ToDateTime(item["huankuanriqi"]) - DateTime.Now).TotalDays;
        //    //        plan = new PBenefitPlan();
        //    //        plan.phkid = item["phkid"].toint32();
        //    //        plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //    //        plan.PlanStatus = 0;//待收益
        //    //        //plan.Principal = 0;
        //    //        // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //    //        plan.Interest = Math.Round(oldtouziedu * chashangdays * rate / 365, 2) + Math.Round(oldtouziedu * chaxiadays * rate / 365, 2);
        //    //        plan.ProjectID = pjktzjlid;
        //    //        plan.UserID = touzirenid;

        //    //        //改变开始日期为当前付息日
        //    //        jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //    //        if (i == hkList.Rows.Count - 1)
        //    //        {
        //    //            plan.Principal = oldtouziedu;
        //    //        }

        //    //        planList.Add(plan);
        //    //        if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //    //        {
        //    //            break;
        //    //        }
        //    //    }
        //    //    else
        //    //    {
        //    //        plan = new PBenefitPlan();
        //    //        plan.phkid = item["phkid"].toint32();
        //    //        plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //    //        plan.PlanStatus = 0;//待收益
        //    //        //plan.Principal = 0;
        //    //        // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //    //        plan.Interest = Math.Round(oldtouziedu * 30 * rate / 365, 2);
        //    //        plan.ProjectID = pjktzjlid;
        //    //        plan.UserID = touzirenid;

        //    //        //改变开始日期为当前付息日
        //    //        jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //    //        if (i == hkList.Rows.Count - 1)
        //    //        {
        //    //            plan.Principal = oldtouziedu;
        //    //        }

        //    //        planList.Add(plan);
        //    //        if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //    //        {
        //    //            break;
        //    //        }
        //    //    }

        //    //}

        //    #endregion

        //    #region 债权购买人收益计划
        //    int tianshu1 = 0;
        //    for (int i = 0; i < hkList.Rows.Count; i++)
        //    {
        //        DataRow item = hkList.Rows[i];
        //        PBenefitPlan plan;
        //        jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //        //计算天数
        //        int intDays = 0;
        //        intDays = (int)(jiexiriqi - DateTime.Now.Date).TotalDays;
        //        if (intDays <= 0)
        //        {
        //            continue;
        //        }
        //        //if ((int)(jiexiriqi - Convert.ToDateTime(bengxiliangqing)).TotalDays <= 0)
        //        //{
        //        //    continue;
        //        //}
        //        tianshu1 += 1;
        //        //如果债券卖出日期是在结息日的前面
        //        if (tianshu1 == 1)
        //        {
        //            //查找上个月还款日期

        //            int chaxiadays = (int)(Convert.ToDateTime(item["huankuanriqi"]) - DateTime.Now).TotalDays;
        //            plan = new PBenefitPlan();
        //            plan.phkid = item["phkid"].toint32();
        //            plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //            plan.PlanStatus = 0;//待收益
        //            //plan.Principal = 0;
        //            // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //            plan.Interest = Math.Round(tzjine * chaxiadays * rate / 365, 2);
        //            plan.ProjectID = newpjktzjlid;
        //            plan.UserID = touzirenid;

        //            //改变开始日期为当前付息日
        //            jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //            if (i == hkList.Rows.Count - 1)
        //            {
        //                plan.Principal = tzjine;
        //            }

        //            planList.Add(plan);
        //            if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //            {
        //                break;
        //            }
        //        }
        //        else
        //        {
        //            plan = new PBenefitPlan();
        //            plan.phkid = item["phkid"].toint32();
        //            plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //            plan.PlanStatus = 0;//待收益
        //            //plan.Principal = 0;
        //            // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //            plan.Interest = Math.Round(tzjine * 30 * rate / 365, 2);
        //            plan.ProjectID = newpjktzjlid;
        //            plan.UserID = touzirenid;

        //            //改变开始日期为当前付息日
        //            jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //            if (i == hkList.Rows.Count - 1)
        //            {
        //                plan.Principal = tzjine;
        //            }

        //            planList.Add(plan);
        //            if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //            {
        //                break;
        //            }
        //        }

        //    }
        //    #endregion

        //    return planList;
        //}

        /// <summary>
        /// 根据债券投资金额获取相应的投资
        /// </summary>
        /// <param name="pjkjbxxid">投资项目编号</param>
        /// <param name="newpjktzjlid">债权购买人投资记录ID</param>
        /// <param name="pjktzjlid">债权出售人原先投资记录ID</param>
        /// <param name="yhids"></param>
        /// <param name="touzirenid">投资人ID</param>
        /// <param name="touzirenid">出售人ID</param>
        /// <param name="tzjine">债权购买额度</param>
        /// <param name="tzjineyh">优惠金额</param>
        /// <param name="rate">利率</param>
        /// <returns></returns>
        //public static IList<PBenefitPlan> GetBenefitPlansByDebtInvestmentAmount(int pjkjbxxid, int newpjktzjlid, int pjktzjlid, string touzirenid, string shouchurenid, decimal tzjine, decimal rate)
        //{
        //    IList<PBenefitPlan> planList = new List<PBenefitPlan>();

        //    rate = rate / 100;
        //    //把债权转让人的投资记录金额变为转变后的金额
        //    jiandansql.execute(" UPDATE pjktzjl SET toubiaojine=toubiaojine-" + tzjine + ",zhaiquanjine=zhaiquanjine+" + tzjine + " WHERE pjktzjlid=" + pjktzjlid);
        //    //把债权转让人的收益计划中未收益的收益计划状态变为“转让为债权”
        //    jiandansql.execute(" UPDATE PBenefitPlan SET PlanStatus=3 WHERE PlanStatus=0 AND pjktzjlid=" + pjktzjlid);

        //    //原标编号pjkjbxxid，原标投资记录pjktzjlid
        //    int zhaiquanrenid = jiandansql.getint("SELECT toubiaorenid FROM pjktzjl WHERE pjktzjlid=" + pjktzjlid);
        //    decimal oldtouziedu = jiandansql.getdecimal("SELECT toubiaojine FROM pjktzjl WHERE pjktzjlid=" + pjktzjlid);
        //    var projrct = jiandansql.getonerow("SELECT * FROM pjkjbxx WHERE pjkjbxxid=" + pjkjbxxid);
        //    var bengxiliangqing = Convert.ToDateTime(projrct["huankuanqixian"]).AddDays(-1);//还款期限
        //    var hkList = jiandansql.gettable(" SELECT * FROM [dbo].[phk] WHERE pjkjbxxid= " + pjkjbxxid);//原标还款计划


        //    int tianshu = 0;
        //    DateTime jiexiriqi;

        //    #region 债权转让人收益计划

        //    for (int i = 0; i < hkList.Rows.Count; i++)
        //    {
        //        DataRow item = hkList.Rows[i];
        //        PBenefitPlan plan;
        //        jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //        //计算天数
        //        int intDays = 0;
        //        intDays = (int)(jiexiriqi - DateTime.Now.Date).TotalDays;
        //        if (intDays <= 0)
        //        {
        //            continue;
        //        }
        //        //int intday = (int)(jiexiriqi - Convert.ToDateTime(bengxiliangqing)).TotalDays;
        //        //if (intday<= 0)
        //        //{
        //        //    continue;
        //        //}
        //        tianshu += 1;
        //        //如果债券卖出日期是在结息日的前面
        //        if (tianshu == 1)
        //        {
        //            //查找上个月还款日期
        //            DateTime shangyuejiexi = Convert.ToDateTime(item["huankuanriqi"]).AddMonths(-1);

        //            int chashangdays = (int)(DateTime.Now - shangyuejiexi).TotalDays;
        //            int chaxiadays = (int)(Convert.ToDateTime(item["huankuanriqi"]) - DateTime.Now).TotalDays;
        //            plan = new PBenefitPlan();
        //            plan.phkid = item["phkid"].toint32();
        //            plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //            plan.PlanStatus = 0;//待收益
        //            //plan.Principal = 0;
        //            // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //            plan.Interest = Math.Round(oldtouziedu * chashangdays * rate / 365, 2) + Math.Round(oldtouziedu * chaxiadays * rate / 365, 2);
        //            plan.ProjectID = pjktzjlid;
        //            plan.UserID = touzirenid;

        //            //改变开始日期为当前付息日
        //            jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //            if (i == hkList.Rows.Count - 1)
        //            {
        //                plan.Principal = oldtouziedu;
        //            }

        //            planList.Add(plan);
        //            if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //            {
        //                break;
        //            }
        //        }
        //        else
        //        {
        //            plan = new PBenefitPlan();
        //            plan.phkid = item["phkid"].toint32();
        //            plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //            plan.PlanStatus = 0;//待收益
        //            //plan.Principal = 0;
        //            // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //            plan.Interest = Math.Round(oldtouziedu * 30 * rate / 365, 2);
        //            plan.ProjectID = pjktzjlid;
        //            plan.UserID = touzirenid;

        //            //改变开始日期为当前付息日
        //            jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //            if (i == hkList.Rows.Count - 1)
        //            {
        //                plan.Principal = oldtouziedu;
        //            }

        //            planList.Add(plan);
        //            if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //            {
        //                break;
        //            }
        //        }

        //    }

        //    #endregion

        //    #region 债权购买人收益计划
        //    int tianshu1 = 0;
        //    for (int i = 0; i < hkList.Rows.Count; i++)
        //    {
        //        DataRow item = hkList.Rows[i];
        //        PBenefitPlan plan;
        //        jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //        //计算天数
        //        int intDays = 0;
        //        intDays = (int)(jiexiriqi - DateTime.Now.Date).TotalDays;
        //        if (intDays <= 0)
        //        {
        //            continue;
        //        }
        //        //if ((int)(jiexiriqi - Convert.ToDateTime(bengxiliangqing)).TotalDays <= 0)
        //        //{
        //        //    continue;
        //        //}
        //        tianshu1 += 1;
        //        //如果债券卖出日期是在结息日的前面
        //        if (tianshu1 == 1)
        //        {
        //            //查找上个月还款日期

        //            int chaxiadays = (int)(Convert.ToDateTime(item["huankuanriqi"]) - DateTime.Now).TotalDays;
        //            plan = new PBenefitPlan();
        //            plan.phkid = item["phkid"].toint32();
        //            plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //            plan.PlanStatus = 0;//待收益
        //            //plan.Principal = 0;
        //            // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //            plan.Interest = Math.Round(tzjine * chaxiadays * rate / 365, 2);
        //            plan.ProjectID = newpjktzjlid;
        //            plan.UserID = touzirenid;

        //            //改变开始日期为当前付息日
        //            jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //            if (i == hkList.Rows.Count - 1)
        //            {
        //                plan.Principal = tzjine;
        //            }

        //            planList.Add(plan);
        //            if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //            {
        //                break;
        //            }
        //        }
        //        else
        //        {
        //            plan = new PBenefitPlan();
        //            plan.phkid = item["phkid"].toint32();
        //            plan.Deadline = Convert.ToDateTime(item["huankuanriqi"]);
        //            plan.PlanStatus = 0;//待收益
        //            //plan.Principal = 0;
        //            // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //            plan.Interest = Math.Round(tzjine * 30 * rate / 365, 2);
        //            plan.ProjectID = newpjktzjlid;
        //            plan.UserID = touzirenid;

        //            //改变开始日期为当前付息日
        //            jiexiriqi = Convert.ToDateTime(item["huankuanriqi"]);
        //            if (i == hkList.Rows.Count - 1)
        //            {
        //                plan.Principal = tzjine;
        //            }

        //            planList.Add(plan);
        //            if (jiexiriqi.Date == Convert.ToDateTime(bengxiliangqing).Date)
        //            {
        //                break;
        //            }
        //        }

        //    }
        //    #endregion

        //    return planList;
        //}
        ///// <summary>
        ///// 
        ///// </summary>
        ///// <param name="projectId"></param>
        ///// <param name="userId"></param>
        ///// <param name="amount"></param>
        ///// <param name="rate"></param>
        ///// <param name="mounth"></param>
        ///// <param name="repayment"></param>
        ///// <param name="dateime"></param>
        ///// <returns></returns>
        //public static IList<PRepaymentSchedule> GetRepaymentScheduleByInvestmentAmount(int projectId, string userId, decimal amount, decimal rate, int mounth, int repayment, DateTime dateime)
        //{
        //    IList<PRepaymentSchedule> plans = new List<PRepaymentSchedule>();
        //    if (repayment == 1)
        //    {
        //        PRepaymentSchedule plan = null;
        //        for (int i = 1; i <= mounth; i++)
        //        {
        //            plan = new PRepaymentSchedule();
        //            if (i != mounth)
        //            {
        //                plan.RepaymentTime = dateime.AddMonths(i);
        //                plan.RepayStatus = 0;
        //                plan.RepayAmounts = 0;
        //                // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //                plan.RepayInterests = Math.Round(amount * 30 * rate / 365, 2);
        //                plan.PorjectID = projectId;
        //                //plan.User = userId;
        //            }
        //            else
        //            {
        //                plan.RepaymentTime = dateime.AddMonths(i);
        //                plan.RepayStatus = 0;
        //                plan.RepayAmounts = amount;
        //                // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //                plan.RepayInterests = Math.Round(amount * 30 * rate / 365, 2);
        //                plan.PorjectID = projectId;
        //                //plan.User = userId;
        //            }
        //            plans.Add(plan);
        //        }
        //    }
        //    else
        //    {
        //        PRepaymentSchedule plan = new PRepaymentSchedule();
        //        plan.RepaymentTime = dateime.AddMonths(mounth);
        //        plan.RepayStatus = 0;
        //        plan.RepayAmounts = amount;
        //        // 利息计算公式: 金额 * 天数 * 利率/365,取两位小数
        //        plan.RepayInterests = Math.Round(amount * 30 * rate / 365, 2);
        //        plan.PorjectID = projectId;
        //        plans.Add(plan);
        //    }
        //    return plans;
        //}

        #endregion
    }

    /// <summary>
    /// 收益计划实体
    /// </summary>
    public class PBenefitPlan
    {
        /// <summary>
        /// //收益计划主键
        /// </summary>
        public int BenefitPlanID;
        /// <summary>
        /// //投资记录主键
        /// </summary>
        public int ProjectID;
        /// <summary>
        /// //融资方还款计划ID
        /// </summary>
        public int phkid;
        /// <summary>
        /// //会员主键
        /// </summary>
        public string UserID;
        /// <summary>
        /// //收益本金
        /// </summary>
        public decimal Principal;
        /// <summary>
        /// //收益利息
        /// </summary>
        public decimal Interest;
        /// <summary>
        /// //收益日期
        /// </summary>
        public DateTime Deadline;
        /// <summary>
        /// //收益状态
        /// </summary>
        public int PlanStatus;
    }
}
